package togoogle;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import java.util.Map;
import java.util.logging.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.repackaged.com.google.common.util.Base64DecoderException;

import com.google.api.client.googleapis.auth.clientlogin.ClientLogin;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.sample.structuredcontent.basicexample.StructuredContentSample;


import togoogle.common.Constant;
import togoogle.common.Util;

@Controller
public class maincontroller {
	private static final Logger log = Logger.getLogger("Main"); 

        @RequestMapping(value = "/togoogle/main",method = RequestMethod.GET)
        public String doGet(String top_appkey, String top_session, 
        		            String top_parameters,String top_sign,
        		            String pageno,Model model) throws Base64DecoderException, IOException {
        	TreeMap<String, String> signparams = new TreeMap<String, String>();
        	signparams.put("top_appkey", top_appkey);
        	signparams.put("top_session", top_session);
        	signparams.put("top_parameters", top_parameters);
        	
        	if(!Util.validateSign(top_sign,top_appkey+top_parameters+top_session,Constant.APP_SERCET)){
        		return "error";
        	}

			Map<String, String> params = new HashMap<String, String>();
			params=Util.convertBase64StringtoMap(top_parameters);
        	model.addAttribute("top_appkey", top_appkey);
        	model.addAttribute("top_session", top_session);
        	model.addAttribute("top_parameters", top_parameters);
        	model.addAttribute("top_sign", top_sign);
        	model.addAttribute("nick", params.get("visitor_nick"));
			Key k = KeyFactory.createKey(acctinfo.class.getSimpleName(), params.get("visitor_nick")); 
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {	
				acctinfo e = pm.getObjectById(acctinfo.class,k);
			}catch(Exception e){
				pm.close();
				log.info(e.getMessage());
				model.addAttribute("regstatus",0);
				return "regist";
			}
/*			StructuredContentSample sample = StructuredContentSample.init("7317466", "http://taokegoo.appspot.com/", "359137638@qq.com", "Zq19821005");
        	try{
        		sample.getProduct("123456");
        	}catch(HttpResponseException e){
        		log.info(e.getResponse().parseAsString());
        		return "regist";
        	}*/
		    pageno=pageno == null || pageno.equals("")? "1":pageno;
		    int begin=(Integer.parseInt(pageno)-1)*20;
		    String sql="select from "+productinfo.class.getName()
		    		    +" where acctid=='"+params.get("visitor_nick")+"'"
		    		    +" order by productid limit "+Integer.toString(begin)+",20";
        	log.info(sql);
		    List<productinfo> productinfos=null;
        	try {
              productinfos = (List<productinfo>) pm.newQuery(sql).execute();
              if(productinfos==null || productinfos.size()==0){
            	  log.info("productinfo is empty!");
            	  return "main";
              }
              log.info("productinfo size:"+productinfos.size());
              log.info("productinfo 0:"+productinfos.get(0).getProductId());
            } catch(Exception e) {
              pm.close();
              log.info(e.getMessage());
              return "error";
            }
        	log.info("go main.jsp");
        	model.addAttribute("productinfos",productinfos);
			return "main";
        }
}